define([
    'jquery',
    'underscore',
    'backbone',
    'module/user/models/UserModel',
    'module/user/models/UserCollection',
    'text!module/user/templates/user_info.html'
], function($, _, Backbone, UserModel, UserCollection, user_info) {

    var UserController = Backbone.View.extend({
        el: $("#page"),
        initialize: function() {
            this.render();
        },
        events: {
            "click #edit_action": "edit_action"
        },
        render: function() {
            this.$el.html(user_info);
            $.ajax({
                url: 'server/api/user/find',
                type: 'GET',
                dataType: "json",
                success: function(data) {
                    $("#id").val(data.id);
                    $("#name").val(data.name);
                    $("#login").val(data.login);
                    $("#password").val(data.password);
                }
            });
        },
        edit_action: function() {
            var self = this;
            var user = new UserModel();
            var userDetails = {
                id: $("#id").val(),
                name: $("#name").val(),
                login: $("#login").val(),
                password: $("#password").val()
            };
            user.save(userDetails, {
                success: function(data) {
                    Backbone.history.navigate("/#");
                }
            });
        }

    });
    return UserController;
});
